<html>
<head><meta charset="utf-8"><title>applying formatting in a rust-analyzer assist · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/applying.20formatting.20in.20a.20rust-analyzer.20assist.html">applying formatting in a rust-analyzer assist</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="221387353"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/applying%20formatting%20in%20a%20rust-analyzer%20assist/near/221387353" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Michael Bryan <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/applying.20formatting.20in.20a.20rust-analyzer.20assist.html#221387353">(Jan 02 2021 at 06:40)</a>:</h4>
<p>I made an assist (<a href="https://github.com/rust-analyzer/rust-analyzer/pull/7131">#7131</a>) and was wondering if I need to make sure the replacement code is formatted correctly.</p>
<p>The doc-comment currently looks like this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="c1">// ```</span>
<span class="c1">// fn add(a: u32, b: u32) -&gt; u32 { a + b }</span>
<span class="c1">// fn main() {</span>
<span class="c1">//     let x = add&lt;|&gt;(1, 2);</span>
<span class="c1">// }</span>
<span class="c1">// ```</span>
<span class="c1">// -&gt;</span>
<span class="c1">// ```</span>
<span class="c1">// fn add(a: u32, b: u32) -&gt; u32 { a + b }</span>
<span class="c1">// fn main() {</span>
<span class="c1">//     let x = { let a = 1; let b = 2; a + b };</span>
<span class="c1">// }</span>
<span class="c1">// ```</span>
</code></pre></div>
<p>But the generate code comes out like this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">add</span><span class="p">(</span><span class="n">a</span>: <span class="kt">u32</span><span class="p">,</span><span class="w"> </span><span class="n">b</span>: <span class="kt">u32</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">u32</span> <span class="p">{</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"></span>
<span class="n">a</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">b</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>If you ignore whitespace the two are syntactically identical, however the generated test fails saying the "text differs".</p>
<p>Should I leave the output as-is and just update my doc-test to expect the unformatted, or is there some sort of formatting routine I can call?</p>



<a name="221395483"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/applying%20formatting%20in%20a%20rust-analyzer%20assist/near/221395483" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/applying.20formatting.20in.20a.20rust-analyzer.20assist.html#221395483">(Jan 02 2021 at 11:14)</a>:</h4>
<p>You can control indentation of syntax nodes to some degree, see <a href="https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/assists/src/handlers/replace_if_let_with_match.rs">https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/assists/src/handlers/replace_if_let_with_match.rs</a> for example</p>



<a name="221395488"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/applying%20formatting%20in%20a%20rust-analyzer%20assist/near/221395488" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/applying.20formatting.20in.20a.20rust-analyzer.20assist.html#221395488">(Jan 02 2021 at 11:15)</a>:</h4>
<p>As in you can set a nodes indentation level and read it out as well.</p>



<a name="221415408"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/applying%20formatting%20in%20a%20rust-analyzer%20assist/near/221415408" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Michael Bryan <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/applying.20formatting.20in.20a.20rust-analyzer.20assist.html#221415408">(Jan 02 2021 at 20:14)</a>:</h4>
<p>Thanks for the pointer,  <span class="user-mention" data-user-id="300586">@Lukas Wirth</span>! I ended up getting exactly <a href="https://github.com/rust-analyzer/rust-analyzer/pull/7131/commits/5b85261f1b7f324db779c797588ae9af9ce984b8">the result I was looking for</a>.</p>
<p>Now I just need to wait for someone more knowledgeable of rust-analyzer to review the PR and make suggestions on how I can generate simpler code for trivial cases.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>